home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Libraries / TransSkel / Demos / Pascal Demos / Hello / Hello.p next >
Encoding:
Text File  |  1994-02-22  |  4.0 KB  |  180 lines  |  [TEXT/PJMM]

  1. { TransSkel "hello, world" application in Pascal }
  2.  
  3. { Presents a window with "Hello, world." displayed centered in the window }
  4. { when the window is active, and "Goodbye, world." when the window is }
  5. { inactive (e.g., when the application is suspended, or when the "About" }
  6. { alert comes up). }
  7.  
  8. { 04 Feb 94 Version 1.00, Paul DuBois }
  9.  
  10. program Hello;
  11.  
  12.     uses
  13.         TransSkel;
  14.  
  15.     const
  16.  
  17. { strings that appear in window }
  18.  
  19.         hello = 'Hello, world.';
  20.         goodbye = 'Goodbye, world.';
  21.  
  22. { resource numbers }
  23.  
  24.         aboutAlrtRes = 1000;                { About box }
  25.         fileMenuNum = skelAppleMenuID + 1;    { File menu }
  26.  
  27. { File menu item numbers }
  28.  
  29.         quit = 1;
  30.  
  31.     var
  32.  
  33.         fileMenu: MenuHandle;
  34.  
  35.         phrase: Str255;                { current phrase }
  36.  
  37.  
  38. {--------------------------------------------------------------------}
  39. { Menu handling procedures }
  40. {--------------------------------------------------------------------}
  41.  
  42. { Handle selection of "About Hello..." item from Apple menu }
  43.  
  44.     procedure DoAppleMenu (item: Integer);
  45.         var
  46.             ignore: Integer;
  47.     begin
  48.         ignore := SkelAlert(aboutAlrtRes, SkelDlogFilter(nil, true), skelPositionOnParentDevice);
  49.         SkelRmveDlogFilter;
  50.     end;
  51.  
  52.  
  53. { Process selection from File menu }
  54.  
  55.     procedure DoFileMenu (item: Integer);
  56.     begin
  57.         case item of
  58.             quit: 
  59.                 SkelStopEventLoop;
  60.         end;
  61.     end;
  62.  
  63.  
  64. { Initialize menus.  Tell TransSkel to process the Apple menu }
  65. { automatically, and associate the proper procedure with the }
  66. { File menu. }
  67.  
  68.     procedure SetupMenus;
  69.         var
  70.             ignore: Boolean;
  71.     begin
  72.         SkelApple('About Hello…', @DoAppleMenu);
  73.         fileMenu := NewMenu(fileMenuNum, 'File');
  74.         AppendMenu(fileMenu, 'Quit/Q');
  75.         ignore := SkelMenu(fileMenu, @DoFileMenu, nil, false, false);
  76.  
  77.         DrawMenuBar;
  78.     end;
  79.  
  80.  
  81. {--------------------------------------------------------------------}
  82. { Window handling procedures }
  83. {--------------------------------------------------------------------}
  84.  
  85. { Draw grow box of window in lower right hand corner }
  86.  
  87.     procedure DrawGrowBox (w: WindowPtr);
  88.         var
  89.             oldClip: RgnHandle;
  90.             r: Rect;
  91.     begin
  92.         r := w^.portRect;
  93.         r.left := r.right - 15;            { draw only in corner }
  94.         r.top := r.bottom - 15;
  95.         oldClip := NewRgn;
  96.         GetClip(oldClip);
  97.         ClipRect(r);
  98.         DrawGrowIcon(w);
  99.         SetClip(oldClip);
  100.         DisposeRgn(oldClip);
  101.     end;
  102.  
  103.  
  104.     procedure Update (resized: Boolean);
  105.         var
  106.             w: WindowPtr;
  107.             r: Rect;
  108.             h, v: Integer;
  109.     begin
  110.         GetPort(w);
  111.         r := w^.portRect;
  112.         EraseRect(r);
  113.         v := (r.top + r.bottom) div 2;
  114.         h := (r.left + r.right - StringWidth(phrase)) div 2;
  115.         MoveTo(h, v);
  116.         DrawString(phrase);
  117.         DrawGrowBox(w);
  118.     end;
  119.  
  120.  
  121. { Normally the grow icon is drawn on a change of activation state, but since }
  122. { the entire portRect is invalidated here, everything will be redrawn by }
  123. { Update(), anyway, including the grow box.  So don't bother here. }
  124.  
  125.     procedure Activate (active: Boolean);
  126.         var
  127.             w: WindowPtr;
  128.     begin
  129.         GetPort(w);
  130.         if (active) then
  131.             phrase := hello
  132.         else
  133.             phrase := goodbye;
  134.         InvalRect(w^.portRect);
  135.     end;
  136.  
  137.  
  138.     procedure Clobber;
  139.         var
  140.             w: WindowPtr;
  141.     begin
  142.         GetPort(w);
  143.         DisposeWindow(w);
  144.     end;
  145.  
  146.  
  147. { Read window from resource file and install handler for it.  Mouse }
  148. { and key clicks are ignored.  There is no close proc since the window }
  149. { doesn't have a close box.  There is no idle proc since nothing is }
  150. { done while the window is in front (all the things that are done are }
  151. { handled by TransSkel). }
  152.  
  153.     procedure WindInit;
  154.         var
  155.             w: WindowPtr;
  156.             bounds: Rect;
  157.             ignore: Boolean;
  158.     begin
  159.         phrase := hello;
  160.         SetRect(bounds, 0, 0, 200, 100);
  161.         if (SkelQuery(skelQHasColorQD) <> 0) then
  162.             w := NewCWindow(nil, bounds, 'Howdy', false, documentProc + 8, WindowPtr(-1), false, 0)
  163.         else
  164.             w := NewWindow(nil, bounds, 'Howdy', false, documentProc + 8, WindowPtr(-1), false, 0);
  165.         SkelPositionWindow(w, skelPositionOnMainDevice, FixRatio(1, 2), FixRatio(1, 5));
  166.         ignore := SkelWindow(w, nil, nil, @Update, @Activate, nil, @Clobber, nil, false);
  167.         TextFont(0);        { select system font in default size }
  168.         TextSize(0);
  169.         SelectWindow(w);
  170.         ShowWindow(w);
  171.     end;
  172.  
  173.  
  174. begin
  175.     SkelInit(nil);
  176.     SetupMenus;
  177.     WindInit;
  178.     SkelEventLoop;
  179.     SkelCleanup;
  180. end.